package com.gtotek.tumchu.customview;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import android.app.Dialog;
import android.content.Context;
import android.text.Html;
import android.text.Spanned;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.gtotek.tumchu.R;
import com.gtotek.tumchu.dao.QuestionEntity;
import com.gtotek.tumchu.dao.QuestionImpl;

public class HelpDialog extends Dialog {

	private QuestionImpl mQuestionImpl;

	private Context mContext;

	private TextView mTvNames;

	private Button mBtnClose;

	private int mIndex = 0;

	private int mSize = 0;

	private final List<QuestionEntity> mQuestionEntity = new ArrayList<QuestionEntity>();

	public HelpDialog(Context context) {
		super(context);
		this.mContext = context;
		setContentView(R.layout.dialog_help);
		getWindow().setBackgroundDrawableResource(
				R.drawable.dialog_full_holo_light);
		Spanned title = Html.fromHtml("<font color='#FF7F27'>"
				+ mContext.getResources().getString(
						R.string.text_title_help_dialog) + "</font>");
		setTitle(title);
		init();
	}

	private void init() {
		this.mTvNames = (TextView) this.findViewById(R.id.tvNames);
		this.mQuestionImpl = new QuestionImpl(mContext);
		this.mBtnClose = (Button) this.findViewById(R.id.btnClose);
		this.mBtnClose.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dismiss();
			}
		});
	}

	private void generateHepl() {
		mQuestionEntity.clear();

		this.mSize = mQuestionImpl.getSize(QuestionImpl.TYPE_ANH_JAV);

		Random random = new Random();

		QuestionEntity entity = mQuestionImpl.getQuestionByPosition(
				this.mIndex, QuestionImpl.TYPE_ANH_JAV);

		QuestionEntity entity1 = mQuestionImpl.getQuestionByPosition(
				random.nextInt(this.mSize), QuestionImpl.TYPE_ANH_JAV);

		QuestionEntity entity2 = mQuestionImpl.getQuestionByPosition(
				random.nextInt(this.mSize), QuestionImpl.TYPE_ANH_JAV);

		mQuestionEntity.add(entity);
		mQuestionEntity.add(entity1);
		mQuestionEntity.add(entity2);

		Collections.shuffle(mQuestionEntity);

		StringBuilder builder = new StringBuilder();

		for (int i = 0; i < mQuestionEntity.size(); ++i) {
			QuestionEntity questionEntity = mQuestionEntity.get(i);
			String name = (i + 1) + ". " + questionEntity.getDapAnCoDau();
			builder.append(name);
			builder.append("\n\n");
		}

		this.mTvNames.setText(builder.toString());
	}

	public void setIndex(int index) {
		this.mIndex = index;
	}

	@Override
	public void show() {
		// TODO Auto-generated method stub
		generateHepl();
		super.show();
	}

}
